package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    ApartmentInfoService apartmentInfoService;
    @Autowired
    LabelInfoService labelInfoService;
    @Autowired
    FacilityInfoService facilityInfoService;
    @Autowired
    FeeValueService feeValueService;
    @Autowired
    AttrValueService attrValueService;
    @Autowired
    LeaseTermService leaseTermService;
    @Autowired
    LeaseAgreementService leaseAgreementService;
    @Autowired
    PaymentTypeService paymentTypeService;
    @Autowired
    BrowsingHistoryService browsingHistoryService;

    @Override
    public IPage<RoomItemVo>  pageRoomItemByQuery(Page<RoomItemVo> page, RoomQueryVo queryVo) {

        return roomInfoMapper.pageRoomItemByQuery(page, queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        //查询 房间基本信息
        RoomInfo roomInfo = getById(id);


        //查询公寓的信息
        ApartmentItemVo apartmentItemVo= apartmentInfoService.getApartmentInfoById(roomInfo.getApartmentId());

        //查询房间的标签
        List<LabelInfo> labelInfoList=labelInfoService.getLabelInfoByRoomId(roomInfo.getId());

        //查询房间的配套
        List<FacilityInfo> facilityInfoList=facilityInfoService.getfacilityInfoByRoomId(roomInfo.getId());

        //查询房间的杂费
        List<FeeValueVo> feeValueVoList=feeValueService.getfeeValueInfoByRoomId(roomInfo.getApartmentId());

        //查询房间的属性
        List<AttrValueVo> attrValueVoList=attrValueService.getAttrValueInfoByRoomId(roomInfo.getId());

        //查询房间的租期
        List<LeaseTerm> leaseTerms= leaseTermService.getLeaseTermInfoByRoomId(roomInfo.getId());

        //查询该房间是否被入住
        boolean res= leaseAgreementService.checkRoomCheckIn(roomInfo.getId());

        //查询房间的支付方式
        List<PaymentType> paymentTypeList=paymentTypeService.getPaymentTypeInfoByRoomId(roomInfo.getId());


        RoomDetailVo roomDetailVo=new RoomDetailVo();

        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setLeaseTermList(leaseTerms);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setIsDelete(roomInfo.getIsDeleted()==1);
        roomDetailVo.setIsCheckIn(res);

        //保存浏览的历史记录
        browsingHistoryService.saveHistory(roomInfo.getId());
        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(IPage<RoomItemVo> page, Long id) {
        return  roomInfoMapper.pageItemByApartmentId(page,id);
    }


}




